from time import sleep
import string, random

import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from rich import print as rprint

def confirm():
    txt = 'C:/Users/tian/Desktop/gogoo/urls.txt'
    try:
        with open(txt, 'r', encoding='utf-8') as f:
            urls = f.read().split('\n')
    except FileNotFoundError:
        urls = ['https://gogoo.club', 'https://googoo.cyou', 'https://niubi.cyou', 'https://hk.gogoo.club', 'https://us.gogoo.club', 'https://kr.gogoo.club'] 
        with open(txt, 'w', encoding='utf-8') as f:
            f.write('\n'.join(urls))
        rprint('urls.txt 文件不存在，已创建')
    rprint('已读取 urls ，正在检查是否可用')
    for url in urls:
        rprint('正在请求：', url)
        try:
            code = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'}, timeout=5).status_code
        except:
            rprint('此网站无法正常访问，检查下一个')
            continue
        if code == 200:
            rprint(f'网址【{url}】可用')
            break

    urls.remove(url)
    urls.insert(0, url)
    rprint('正在重写 urls.txt')
    with open(txt, 'w', encoding='utf-8') as f:
        f.write('\n'.join(urls))
    return url

class Browser:
    def __init__(self):
        options = Options()
        options.page_load_strategy = 'eager'
        options.add_argument('--start-maximized')
        options.add_argument('--incognito')
        self.browser = webdriver.Chrome(options=options)

    def input_content(self, id, con=0):
        content = ''
        for i in random.choices(string.ascii_letters, k=8):
            content += i
        if con == 1:
            content += '@gmail.com'
        elif con == 2:
            content = 'https://www.vzone.pw/auth/register'
        self.browser.find_element(By.ID, id).send_keys(content)

    def regester(self, url, site='go'):
        self.browser.get(url)
        Browser.input_content(self, 'name')
        if site == 'go':
            Browser.input_content(self, 'email', con=1)
        elif site == 're':
            Browser.input_content(self, 'email')
        Browser.input_content(self, 'passwd', con=2)
        Browser.input_content(self, 'repasswd', con=2)
        WebDriverWait(self.browser,60).until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_radar_tip')))
        self.browser.find_element(By.CLASS_NAME, 'geetest_radar_tip').click()
        WebDriverWait(self.browser,60).until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '验证成功'))
        self.browser.find_element(By.ID, 'register-confirm').click()
        sleep(2)
        self.browser.find_element(By.XPATH, '//div[@class="swal2-actions"]/button').click()

    def go(self, url):
        Browser.regester(self, url)
        WebDriverWait(self.browser,30).until(EC.title_is('首页 — Go加速'))
        sleep(1.5)
        self.browser.find_element(By.XPATH, '//div[@class="modal-content"]//button').click()        # 关闭重要通知
        sleep(1.5)
        self.browser.find_element(By.XPATH, '//div[@class="wizard-step wizard-step-active"]').click()          # 前往商店订购免费试用
        self.browser.execute_script('window.scrollTo(0,500)')
        self.browser.find_element(By.XPATH, '//div[@class="row"]/div[1]//div[@class="pricing-cta"]').click()    # 订购试用套餐
        sleep(1.5)
        self.browser.find_element(By.XPATH, '//div[@class="swal2-actions"]/button').click()         # 关闭确认窗口
        sleep(2)    # 等待自动返回主页
        self.browser.find_element(By.XPATH, '//div[@class="modal-content"]//button').click()        # 关闭重要通知
        self.browser.execute_script('window.scrollTo(0,700)')
        global subscribe_link
        subscribe_link = self.browser.find_element(By.XPATH, '//div[@class="buttons"]/a[5]').get_attribute('data-clipboard-text')
        rprint('[green]{}\n订阅链接：{}\n{}'.format('-'*90, subscribe_link, '-'*90))

    def re(self, url):
        Browser.regester(self, url, site='re')
        WebDriverWait(self.browser,30).until(EC.title_is('首页 — RE'))
        self.browser.execute_script('window.scrollTo(0,700)')
        subscribe_link = self.browser.find_element(By.XPATH, '//div[@class="buttons"]/a[5]').get_attribute('data-clipboard-text')
        rprint('[green]{}\n订阅链接：{}\n{}'.format('-'*110, subscribe_link, '-'*110))

go_url = confirm() + '/auth/register'
re_url = 'https://www.reoen.top/auth/register'

choice = input('需要先注册 Go 机场吗？（需要=>回车，不需要=>其他）：\n')
B = Browser()
if choice == '':
    B.go(go_url)
    next = input('打开代理后，回车继续注册 10G 机场：')
B.re(re_url)
next = input('回车关闭窗口：')


# 网址发布页：https://x-go.gitbook.io/untitled/